<?php
class Shiporder_main extends CI_Controller{
    public function __consruct(){
        parent::__construct();
        $this->load->helper('form');
          $this->load->helper('validation_helper');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('user_model');
    }
    public function index($con=' '){
        
        $ar=array('add_user','add_couriers','list_user','list_courier');
        $this->template->write_view('header', 'header', array('title'=>'SHIP ORDER'), $overwrite = FALSE);
      
         $nav['navigation']=array('home');
        
       //The side bar visible only when the user login
        if($this->session->userdata('user_id')>0)
            $this->template->write_view('sidebar', 'links', '', $overwrite = FALSE);
        
        // The content part of (add_courier,add_user , list_user,list_courier) is only when the user login. u cant come direclty by typing url
         
        if(in_array($con,$ar)){
             $id=$this->session->userdata('user_id');           
              if(empty($id) or $id <= 0) {
                $con='home';               
              }
        }
       // For Navigation with out using external Library
        if($con !='home')
            array_push($nav['navigation'],$con);
         
         /* NAvigation using external breadcrumb library
         if($con !='home'){
            $u= base_url().'shiporder_main/index/'.$con;
           
            $this->breadcrumb->addElement($con,$u, array('class'=>'breadcrumb_class_last'));
         }
          $barr['navigation']=$this->breadcrumb->get(); 
         */
            
        $this->template->write_view('content', $con, '', $overwrite = FALSE);
       
        $this->template->write_view('navigation', 'navigation', $nav, $overwrite = FALSE);
        $this->template->write_view('footer', 'footer', '', $overwrite = FALSE);
        $this->template->render();
    }  
    
     public function login(){
        
        
         $this->form_validation->set_rules(
            array(
                array('field' => 'user_email', 'label' => 'Email', 'rules' => 'trim|required|valid_email|callback_email_check'),
                array('field' => 'user_password', 'label' => 'Password', 'rules' => 'trim|required|callback_password_check'), 
                ));
         if ($this->form_validation->run()==FALSE)
        {
               
              	$data['message'] = (validation_errors()) ? validation_errors() :'';
                
             $this->template->write_view('content', 'user_login', $data, $overwrite = FALSE);
             $this->template->render();
        } else
        {
                $cond['user_email']=$this->input->post('user_email');
              $cond['user_password']=MD5($this->input->post('user_password'));
              $this->load->model('user_model');
              $r=$this->user_model->get_users($cond)->num_rows();
              
              if($r>0){
                $this->session->set_userdata('user_id',$r);
                redirect('shiporder_main/index/login_success');
              }            
              else
                redirect('shiporder_main/index/');
        }   
     } 
}
 ?>